package zhaotupian;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

public class YeMianTiQu extends Mapper<LongWritable, Text,Text,NullWritable> {


    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String s = value.toString();
        //        if(o.contains("img")) {
//            String[] s = o.split("src=");
//        }
//        String s = value.toString();
//        if(s.contains("width")){
//            String[] ss =s.split("width");
//        }
//        String ss = value.toString();
        boolean a =  s.contains("img src=");
//        boolean F = s.contains("width");
        if(a ) {
            //转义谁在谁前面写反斜杠（\）
            String[] line = s.split("img src=\"");
            String b = line[1];
            String[] c = b.split("width");
            String d = c[0];

            context.write(new Text(d),NullWritable.get());
        }else {
            return;
        }
    }

    public static void main(String[] args) throws IOException,ClassNotFoundException,InterruptedException{
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(YeMianTiQu.class);
        job.setMapperClass(YeMianTiQu.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        File file = new File("f:/all/a/tpa");
        if(file.exists()){
            FileUtils.deleteDirectory(file);
        }

        FileInputFormat.addInputPath(job, new Path("f:/all/a/tp/a.txt"));
        FileOutputFormat.setOutputPath(job, new Path("f:/all/a/tpa"));

        job.setNumReduceTasks(1);


        job.waitForCompletion(true);
    }
}

